package com.statistics.service.Impl;

import com.statistics.cloudpower.dao.*;
import com.statistics.cloudpower.entity.TblEquipment;
import com.statistics.service.StatisticsService;
import com.statistics.thread.ElectricityStatisticsThread;
import com.statistics.thread.ElectricityStatisticsWeekThread;
import com.statistics.thread.WaterStatisticsThread;
import com.statistics.thread.WaterStatisticsWeekThread;
import com.statistics.tools.Constants;
import com.statistics.yfdata.dao.DataElectricityRepository;
import com.statistics.yfdata.dao.DataWaterRepository;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author MәӧωρaЯsε
 * @date 2017/7/11.
 */
@Service
public class StatisticsServiceImpl implements StatisticsService {

    private int day_size = 30;
    private int week_size = 30;
    private int month_size = 30;

    private final org.slf4j.Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private TblElectricityStatisticsDayDAO tblElectricityStatisticsDayDAO;
    @Autowired
    private TblElectricityStatisticsWeekDAO tblElectricityStatisticsWeekDAO;
    @Autowired
    private TblElectricityStatisticsMonthDAO tblElectricityStatisticsMonthDAO;

    @Autowired
    private TblEquipmentDAO tblEquipmentDAO;
    @Autowired
    private DataElectricityRepository dataElectricityRepository;


    @Override
    public void countEveryDay() throws ParseException {
        Long equipment_count = tblEquipmentDAO.count(Constants.ELECTRICITY_TYPE);
        String uuid = UUID.randomUUID().toString();
        int fragment_number = 1;
        logger.info("共有{}个电量设备",equipment_count);

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE,  - 1);
        String month = String.format("%02d", cal.get(Calendar.MONTH) + 1);
        for (long start = 0; start < equipment_count; start += day_size, fragment_number++) {
            //读取count个设备
            List<TblEquipment> tblEquipment = tblEquipmentDAO.findPageBySql(Constants.ELECTRICITY_TYPE, start, day_size);
            //command为任务模式-每日、每周、每月
            new ElectricityStatisticsThread(Constants.EVERY_DAY, tblEquipment, tblElectricityStatisticsDayDAO, uuid, fragment_number,
                    dataElectricityRepository, month).start();
        }
    }

    @Override
    public void countEveryWeek() throws ParseException {

        Long equipment_count = tblEquipmentDAO.count(Constants.ELECTRICITY_TYPE);
        String uuid = UUID.randomUUID().toString();
        int fragment_number = 1;
        logger.info("共有{}个电量设备",equipment_count);

        for (long start = 0; start < equipment_count; start += week_size, fragment_number++) {

            //读取count个设备
            List<TblEquipment> tblEquipment = tblEquipmentDAO.findPageBySql(Constants.ELECTRICITY_TYPE, start, week_size);

            //command为任务模式-每日、每周、每月
            new ElectricityStatisticsWeekThread(tblEquipment,
                    tblElectricityStatisticsWeekDAO, uuid, fragment_number,
                    dataElectricityRepository).start();
        }
    }

    @Override
    public void countEveryMonth() {
        Long equipment_count = tblEquipmentDAO.count(Constants.ELECTRICITY_TYPE);
        String uuid = UUID.randomUUID().toString();
        int fragment_number = 1;
        logger.info("共有{}个电量设备",equipment_count);

        Date date = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, -1);
        String month = String.format("%02d", (cal.get(Calendar.MONTH) + 1));
        for (long start = 0; start < equipment_count; start += month_size, fragment_number++) {

            //读取count个设备
            List<TblEquipment> tblEquipment = tblEquipmentDAO.findPageBySql(Constants.ELECTRICITY_TYPE, start, month_size);

            //command为任务模式-每日、每周、每月
            new ElectricityStatisticsThread(Constants.EVERY_MONTH,
                    tblEquipment,
                    tblElectricityStatisticsMonthDAO, uuid, fragment_number,
                    dataElectricityRepository, month).start();
        }
    }

}
